import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login.vue'
const Users = () => import('@/views/users/Users.vue')
const Roles = () => import('@/views/power/Roles.vue')
const Categories = () => import('@/views/goods/Categories.vue')
const List = () => import('@/views/goods/List.vue')
const Add = () => import('@/views/goods/Add.vue')
const Reports = () => import('@/views/Reports/Reports.vue')
const Order = () => import('@/views/order/Order.vue')

Vue.use(VueRouter)

const routes = [{
  path: '/',
  redirect: '/login'
},
{
  path: '/login',
  component: Login
},
{
  path: '/home',
  component: () => import('@/views/layout/Home.vue'),
  redirect: '/welcome',
  children: [{
    path: '/welcome',
    component: () => import('@/views/layout/Welcome.vue')
  },
  {
    path: '/users',
    component: Users,
    meta: {
      title: '用户管理'
    }
  },
  {
    path: '/roles',
    component: Roles,
    meta: {
      title: '角色管理'
    }
  },
  {
    path: '/categories',
    component: Categories,
    meta: {
      title: '商品分类'
    }
  },
  {
    path: '/goods',
    component: List,
    meta: {
      title: '商品列表'
    }
  },
  {
    path: '/goods/add',
    component: Add,
    meta: {
      title: '添加商品'
    }
  },
  {
    path: '/orders',
    component: Order,
    meta: {
      title: '订单管理'
    }
  },
  {
    path: '/reports',
    component: Reports
  }
  ]
}
]

const router = new VueRouter({
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const token = window.sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})

export default router
